package com.chinairi.mall.util;

import com.thoughtworks.xstream.core.util.Base64Encoder;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

/**
 * @author ypj
 * @date 2020/9/1 9:00
 */
@Component
public class EmailUtil {
	
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     *
     * @param toEmail  接收邮箱账号
     * @param subject   邮件主题
     * @param operation  操作内容
     * @return
     */
    public String getEmail(String toEmail,String subject,String operation) {
        String code = RandomStringUtils.randomNumeric(6);
        Base64Encoder base64 = new Base64Encoder();
        String secretCode = base64.encode(code.getBytes());
        String contText = "<html>\r\n" +
            "	<head>\r\n" +
            "    <base target=\"_blank\" />\r\n" +
            "    <style type=\"text/css\">::-webkit-scrollbar{ display: none; }</style>\r\n" +
            "    <style id=\"cloudAttachStyle\" type=\"text/css\">#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}</style>\r\n" +
            "    <style id=\"blockquoteStyle\" type=\"text/css\">blockquote{display:none;}</style>\r\n" +
            "    <style type=\"text/css\">\r\n" +
            "        body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}\r\n" +
            "        td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}\r\n" +
            "        pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}\r\n" +
            "        th,td{font-family:arial,verdana,sans-serif;line-height:1.666}\r\n" +
            "        img{ border:0}\r\n" +
            "        header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}\r\n" +
            "        blockquote{margin-right:0px}\r\n" +
            "    </style>\r\n" +
            "</head>\r\n" +
            "<body tabindex=\"0\" role=\"listitem\">\r\n" +
            "<table width=\"700\" border=\"0\" align=\"center\" cellspacing=\"0\" style=\"width:700px;\">\r\n" +
            "    <tbody>\r\n" +
            "    <tr>\r\n" +
            "        <td>\r\n" +
            "            <div style=\"width:680px;padding:0 10px;margin:0 auto;\">\r\n" +
            "                <div style=\"line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;\">\r\n" +
            "                    <strong style=\"display:block;margin-bottom:15px;\">尊敬的用户：<span style=\"color:#f60;font-size: 16px;\"></span>您好！</strong>\r\n" +
            "                    <strong style=\"display:block;margin-bottom:15px;\">\r\n" +
            "                        您正在进行<span style=\"color: red\">"+operation+"</span>操作，请在验证码输入框中输入：<span style=\"color:#f60;font-size: 24px\">" + code + "</span>，以完成操作。\r\n" +
            "                    </strong>\r\n" +
            "                </div>\r\n" +
            "                <div style=\"margin-bottom:30px;\">\r\n" +
            "                    <small style=\"display:block;margin-bottom:20px;font-size:12px;\">\r\n" +
            "                        <p style=\"color:#747474;\">\r\n" +
            "                            注意：此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作，请及时登录并修改密码以保证帐户安全\r\n" +
            "                            <br>（工作人员不会向你索取此验证码，请勿泄漏！)\r\n" +
            "                        </p>\r\n" +
            "                    </small>\r\n" +
            "                </div>\r\n" +
            "            </div>\r\n" +
            "            <div style=\"width:700px;margin:0 auto;\">\r\n" +
            "                <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">\r\n" +
            "                    <p>此为系统邮件，请勿回复<br>\r\n" +
            "                        请保管好您的邮箱，避免账号被他人盗用\r\n" +
            "                    </p>\r\n" +
            "                    <p>北京宏图云锦科技有限公司</p>\r\n" +
            "                </div>\r\n" +
            "            </div>\r\n" +
            "        </td>\r\n" +
            "    </tr>\r\n" +
            "    </tbody>\r\n" +
            "</table>\r\n" +
            "</body>\r\n" +
            "</html>";
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("service@cloudscmp.com");
            helper.setTo(toEmail);
            helper.setSubject(subject);
            helper.setText(contText, true);
            javaMailSender.send(message);
            System.out.println("html格式邮件发送成功");
        } catch (Exception e) {
            System.out.println("html格式邮件发送失败");
        }
        return secretCode;
    }
}
